package com.aorise.service.bayonet.impl;

import com.aorise.mapper.bayonet.BayonetStatisticsMapper;
import com.aorise.model.bayonet.BayonetGroup;
import com.aorise.model.bayonet.BayonetStatisticsModel;
import com.aorise.service.bayonet.BayonetService;
import com.aorise.service.bayonet.BayonetStatisticsService;
import com.aorise.utils.ConstDefine;
import com.aorise.utils.Page;
import com.aorise.utils.excel.ExcelUtil;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * @Auther: zhouhao
 * @Date: 2019/6/27
 * @Description:    卡口统计报表Service实现类
 */
@Service
public class BayonetStatisticsServiceImpl implements BayonetStatisticsService{

    @Autowired
    BayonetStatisticsMapper bayonetStatisticsMapper;

    @Autowired
    BayonetService bayonetService;

    /**
     * 卡口统计报列表
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param size   页大小
     * @param num   页索引
     * @return
     */
    @Override
    public Page getBayonetStatistics(String startTime, String endTime,String size,String num,String groupId) {

        Page page = new Page();
        String pageNum =num;
        String pageSize = size;
        pageSize = StringUtils.isBlank(pageSize) ? "10" : pageSize;
        pageNum = StringUtils.isBlank(pageNum) ? "1" : pageNum;
        page.setCurrentPage(Long.valueOf(pageNum));
        page.setEveryPage(Long.valueOf(pageSize));
        
        Map<Object, Object> map = Maps.newHashMap();
        if(StringUtils.isNotBlank(startTime)){
            map.put("startTime",startTime);
        }
        if(StringUtils.isNotBlank(endTime)){
            map.put("endTime",endTime);
        }
        if(StringUtils.isNotBlank(groupId)){
            map.put("groupId",groupId);
        }
        long count =bayonetStatisticsMapper.getBayonetStatisticsCount(map);
        if (count > 0) {
            page.setTotalCount(count);
            page.setTotalPage(count, page.getEveryPage());
            if (page.getCurrentPage() > 0 && page.getEveryPage() > 0) {
                map.put("beginIndex", page.getBeginIndex());
                map.put("endinIndex", page.getEndinIndex());
                page.setList(bayonetStatisticsMapper.getBayonetStatistics(map));
            }
        } else {
            page.setTotalCount(0L);
        }
        return page;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    @Override
    public void bayonetStatisticsService() {
        List<BayonetGroup> bayonetGroups = bayonetService.getBayonetGroup();
        HashMap<Object, Object> map = Maps.newHashMap();
        map.put("state", ConstDefine.STATE_ABLE);
        List<BayonetStatisticsModel> bayonetList = Lists.newArrayList();
        for(BayonetGroup group:bayonetGroups){
            map.put("groupId",group.getId());
            BayonetStatisticsModel bayonetStatistics =bayonetStatisticsMapper.getbayonetStatistic(map);
            bayonetStatistics.setBaOfline(bayonetStatistics.getBaTotal()-bayonetStatistics.getBaOnline());
            bayonetList.add(bayonetStatistics);
            for (BayonetGroup cgroup:group.getChildBayonetGroup()){
                map.put("groupId",cgroup.getId());
                BayonetStatisticsModel child = bayonetStatisticsMapper.getchildBayonetStatistic(map);
                child.setBaOfline(child.getBaTotal()-child.getBaOnline());
                bayonetList.add(child);
            }
        }
        getBoolean(bayonetStatisticsMapper.insertayonetStatistics(bayonetList));
    }

    @Override
    public void exportBayonetStatistics(String startTime, String endTime, String groupId, HttpServletResponse response) {
        List<String[]> dataList = Lists.newArrayList();
        Map<Object, Object> map = Maps.newHashMap();
        if(StringUtils.isNotBlank(startTime)){
            map.put("startTime",startTime);
        }
        if(StringUtils.isNotBlank(endTime)){
            map.put("endTime",endTime);
        }
        if(StringUtils.isNotBlank(groupId)){
            map.put("groupId",groupId);
        }
        List<LinkedHashMap<String,String>> bayonetStatistics =bayonetStatisticsMapper.exportBayonetStatistics(map);
        LinkedHashMap linkedHashMap =null;
        int j =0;
        for(int i =0; i<bayonetStatistics.size();i++){
            linkedHashMap = bayonetStatistics.get(i);
            String[] dataString = new String[linkedHashMap.size()];
            Iterator iterator = linkedHashMap.entrySet().iterator();
            j =0;
            while (iterator.hasNext()){
                Map.Entry next = (Map.Entry) iterator.next();
                dataString[j] =String.valueOf( next.getValue());
                j++;
            }
            dataList.add(dataString);
        }

        ExcelUtil.writeExcel(dataList, null,6, 3, "卡口统计报表","bayonetStatistics.xlsx", response);
    }
}
